package com.tryee.wx.config.utils;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.core.RedisTemplate;

import java.util.concurrent.TimeUnit;

/**
 * 微信redis工具类
 */
@Configuration
public class RedisUtils {

    private final String REDISKEY = "weixin:";

    private final String TOKENACCESS = "weixin:token";

    @Autowired
    private RedisTemplate<String, String> redisTemplate;


    public String getString(String key) {
        return redisTemplate.opsForValue().get(REDISKEY + key);
    }

    public void setString(String key, String value) {
        redisTemplate.opsForValue().set(REDISKEY + key, value);
        redisTemplate.expire(REDISKEY + key, 2, TimeUnit.HOURS);
    }

    public void setString(String key, String value,TimeUnit timeUnit,int num) {
        redisTemplate.opsForValue().set(REDISKEY + key, value);
        redisTemplate.expire(REDISKEY + key, num, timeUnit);
    }

    /**
     * 获取微信token
     * @return
     */
    public String getToken() {
        return redisTemplate.opsForValue().get(TOKENACCESS);
    }

    /**
     * 更新微信token
     * @param value
     */
    public void setToken(String value) {
        redisTemplate.opsForValue().getAndSet(TOKENACCESS, value);
        redisTemplate.expire(TOKENACCESS, 2, TimeUnit.HOURS);
    }

}
